package com.edu.policy.controller;

import com.edu.policy.common.api.CommonResult;
import com.edu.policy.domin.FunctionParseResult;
import com.edu.policy.domin.PageDto;
import com.edu.policy.model.DmsDevices;
import com.edu.policy.model.DmsFunctionCodes;
import com.edu.policy.service.AppDmsFuncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RequiredArgsConstructor
@Controller
@Api(tags = "DmsFuncController")
@RequestMapping("/function")
public class DmsFuncController {
    private final AppDmsFuncService dmsFuncService;
    @ApiOperation("添加功能码信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> create(@RequestBody DmsFunctionCodes dmsFunctionCodes) {
        boolean count = dmsFuncService.save(dmsFunctionCodes);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改功能码信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> update(@PathVariable Integer id,
                                                 @RequestBody DmsFunctionCodes dmsFunctionCodes) {
        dmsFunctionCodes.setId(id);
        boolean count = dmsFuncService.updateById(dmsFunctionCodes);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<DmsFunctionCodes> getItem(@PathVariable Integer id) {
        DmsFunctionCodes dmsDevices = dmsFuncService.getById(id);
        return CommonResult.success(dmsDevices);
    }

    @ApiOperation("根据ID删除功能码信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> delete(@PathVariable Integer id) {
        boolean count = dmsFuncService.removeById(id);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询功能码信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageDto<DmsFunctionCodes>> list(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage) {
        PageDto<DmsFunctionCodes> funcList = dmsFuncService.list(pageSize, currentPage);
        return CommonResult.success(funcList);
    }

    @ApiOperation("功能能码解析信息")
    @RequestMapping(value = "/parse", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<FunctionParseResult>> parse(@RequestParam(value = "deviceId") String deviceId,
                                                        @RequestParam(value = "funcCode") String funcCode) {
        List<FunctionParseResult> parseList = dmsFuncService.parse(deviceId, funcCode);
        return CommonResult.success(parseList);
    }
}
